<?php
/**
 * Created by PhpStorm.
 * User: Cloud
 * Date: 2018/4/14
 * Time: 14:42
 */

class News extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('pagination');
        $this->load->model('bill_model');
        $this->load->model('goods_model');
    }

    //标为已读
    public function readed()
    {
        $customer_id = $this->session->customer_id;
        if (isset($_GET['id'])) {
            $read['read_status'] = 1;
            $this->db->where(array('id' => $_GET['id'], 'receiver_customer_id' => $customer_id))->update('message_history', $read);
        }
    }

//消息通知
    public function news_pull()
    {
        $customer_id = $this->session->customer_id;
//标记为已读
        if (isset($_GET['status'])) {
            $newsid['id'] = $_GET['id'];
            $read['read_status'] = 1;
            $this->db->where(array('id' => $newsid['id'], 'receiver_customer_id' => $customer_id))->update('message_history', $read);
            exit('成功设置消息为已读');
            $this->session->set_flashdata('msg', '成功设置消息为已读');
            redirect('news/news_pull?p=' . $_GET['p'] . '&statu=' . $_GET['statu']);
            exit;
        }
//删除
        if (isset($_GET['del'])) {
            $newsid['id'] = $_GET['del'];
            $this->db->where(array('id' => $newsid['id'], 'receiver_customer_id' => $customer_id))->delete('message_history');
            exit('成功删除消息');
            $this->session->set_flashdata('msg', '成功删除消息');
            redirect('news/news_pull?p=' . $_GET['p'] . '&statu=' . $_GET['statu']);
            exit;
        }
//全部设为已读
        if (isset($_GET['read_status'])) {
            $news['read_status'] = 1;
            $query = $this->db->query("SELECT * FROM message_history WHERE receiver_customer_id=" . $customer_id);
            $newspull = $query->result_array();
            foreach ($newspull as $quer) {
                $this->db->where('receiver_customer_id', $quer['receiver_customer_id'])->update('message_history', $news);
            }
            exit('成功设置全部消息为已读');
            $this->session->set_flashdata('msg', '成功设置全部消息为已读');
            redirect('news/news_pull?p=' . $_GET['p'] . '&statu=' . $_GET['statu']);
            exit;
        }
        //全部删除
        if (isset($_GET['restatus'])) {
            $news['read_status'] = $_GET['restatus'];
            $this->db->where(array('read_status' => $news['read_status'], 'receiver_customer_id' => $customer_id))->delete('message_history');
            if ($news['read_status'] == 1) {
                exit('成功删除所有已读消息');
                $this->session->set_flashdata('msg', '成功删除所有已读消息');
            }
            if ($news['read_status'] == 0) {
                exit('成功删除所有未读消息');
                $this->session->set_flashdata('msg', '成功删除所有未读消息');
            }
            redirect('news/news_pull?&statu=' . $_GET['statu']);
            exit;
        }

//已读和未读查询
        if (isset($_GET['statu']) == 1 || isset($_GET['statu']) == 2) {
            $read = $_GET['statu'];
            $newspull = $this->db->select('mes.*')->where('receiver_customer_id', $customer_id)->where('read_status', $read)->from('message_history mes')->order_by('id', 'desc')->get();
//                ->join('app_customer cut', 'cut.id=mes.send_customer_id')
        } else {
            $read = 0;
            $newspull = $this->db->select('mes.*')->where('receiver_customer_id', $customer_id)->where('read_status', $read)->from('message_history mes')->order_by('id', 'desc')->get();
//                ->join('app_customer cut', 'cut.id=mes.send_customer_id')
        }
        $newspull = $newspull->result_array();
        if (count($newspull)) {
            foreach ($newspull as $key => $new) {
                if ($new['send_customer_id']) {
                    $name = $this->db->select('name')->where('id', $new['send_customer_id'])->from('app_customer')->get()->row_array();
                    $newspull[$key]['name'] = $name['name'];
                } else {
                    $newspull[$key]['name'] = '平台';
                }
            }
        }
        $newspull['total'] = count($newspull);

//分页
        $listRows = 10;                    //每页显示条数
        $num = ceil($newspull['total'] / $listRows);
        if ($num = floor($num)) {
            $num = floor($num);
        } else {
            floor($num) + 1;
        }
        $nowPage = isset($_GET['p']) ? $_GET['p'] : 1;  //当前页
        if ($nowPage < $num) {                                              //下一页
            $data['nexturl'] = base_url('news/news_pull') . '?p=' . ($nowPage + 1) . '&statu=' . $read;
            //$data['nexturl'] = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?p=' . ($nowPage + 1) . '&statu=' . $read;
        } else {
            $data['nexturl'] = 'javascript:;';
        }
        if ($nowPage > 1) {                              //上一页
            $data['preurl'] = base_url('news/news_pull') . '?p=' . ($nowPage - 1) . '&statu=' . $read;
            // $data['preurl'] = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '/?p=' . ($nowPage - 1) . '&statu=' . $read;
        } else {
            $data['preurl'] = 'javascript:;';
        }
        $firstRow = $listRows * ($nowPage - 1); //后一页
        if (isset($_GET['statu']) == 1 || isset($_GET['statu']) == 2) {
            $read = $_GET['statu'];
            $newspull = $this->db->select('mes.*')->where('receiver_customer_id', $customer_id)->where('read_status', $read)->from('message_history mes')->order_by('id', 'desc')
//                ->join('app_customer cut', 'cut.id=mes.send_customer_id')
                ->limit($listRows, $firstRow)->get();

        } else {
            $read = 0;
            $newspull = $this->db->select('mes.*')->where('receiver_customer_id', $customer_id)->where('read_status', $read)->from('message_history mes')->order_by('id', 'desc')
//                ->join('app_customer cut', 'cut.id=mes.send_customer_id')
                ->limit($listRows, $firstRow)->get();
        }
        $newspull = $newspull->result_array();
        if (count($newspull)) {
            foreach ($newspull as $key => $new) {
                if ($new['send_customer_id']) {
                    $name = $this->db->select('name')->where('id', $new['send_customer_id'])->from('app_customer')->get()->row_array();
                    $newspull[$key]['name'] = $name['name'];
                } else {
                    $newspull[$key]['name'] = '平台';
                }
            }
        }
        $data['total_page'] = ceil(count($newspull) / 10);
        $data['nowPage'] = $nowPage;
        $data['newspull'] = $newspull;
        $data['read'] = count($newspull);
        $data['status'] = $read;
        $this->load->view('news/news_pull', $data);
    }

//账期提醒
    public function news_bill()
    {
        $customer_id = $this->session->customer_id;
        $period = $this->db->where('customer_id', $customer_id)->from('app_admin')->get();
        $period = $period->row_array();
//        print_r($period);
        //逐次月账期
        if ($period['account_period_type'] == 1) {
            //状态读取
            if ($_GET) {
                if (isset($_GET['statu'])) {
                    if ($_GET['statu'] == 0 || $_GET['statu'] == 1 || $_GET['statu'] == 2) {
                        $status = $_GET['statu'];
                        $bill = $this->db->select('b.*,m.*,a.*')
                            ->where('a.customer_id', $customer_id)
                            ->where('status', $status)
                            ->where('bill_date != ', NULL)
                            ->from('app_admin a')
                            ->join('app_bill b', 'a.customer_id=b.customer_id')
                            ->join('app_bill_month m', 'b.id=m.bill_id')
                            ->get();
                    }
                }
            } else {
                $status = 0;
                $bill = $this->db->select('a.*')
                    ->where('a.customer_id', $customer_id)
                    ->where('status', $status)
                    ->where('bill_date != ', NULL)
                    ->from('app_admin a')
                    ->join('app_bill b', 'a.customer_id=b.customer_id')
                    ->join('app_bill_month m', 'b.id=m.bill_id')
                    ->get();
            }

            $bill = $bill->result_array();

            $bill['total'] = count($bill);

            //分页
            $listRows = 10;                    //每页显示条数
            $num = ceil($bill['total'] / $listRows);
            if ($num = floor($num)) {
                $num = floor($num);
            } else {
                $num = floor($num) + 1;
            }
            $nowPage = isset($_GET['p']) ? $_GET['p'] : 1;  //当前页
            if ($nowPage < $num) {
                $data['nexturl'] = base_url('news/news_bill_month') . '?p=' . ($nowPage + 1) . '&statu=' . $status;//下一页
//                $data['nexturl'] = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?p=' . ($nowPage + 1) . '&statu=' . $status;
            } else {
                $data['nexturl'] = 'javascript:;';
            }
            if ($nowPage > 1) {
                $data['preurl'] = base_url('news/news_bill_month') . '?p=' . ($nowPage - 1) . '&statu=' . $status; //上一页
//                $data['preurl'] = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?p=' . ($nowPage - 1) . '&statu=' . $status;
            } else {
                $data['preurl'] = 'javascript:;';
            }
            $firstRow = $listRows * ($nowPage - 1); //后一页
            if ($_GET) {
                if (isset($_GET['statu'])) {
                    if ($_GET['statu'] == 0 || $_GET['statu'] == 1 || $_GET['statu'] == 2) {
                        $status = $_GET['statu'];
                        $bill = $this->db->select('b.*,m.*,a.*')
                            ->where('a.customer_id', $customer_id)
                            ->where('status', $status)
                            ->where('bill_date != ', NULL)
                            ->from('app_admin a')
                            ->join('app_bill b', 'a.customer_id=b.customer_id')
                            ->join('app_bill_month m', 'b.id=m.bill_id')
                            ->limit($listRows, $firstRow)->get();
                    }
                }
            } else {
                $status = 0;
                $bill = $this->db->select('b.*,m.*,a.*')->where('a.customer_id', $customer_id)
                    ->where('status', $status)
                    ->where('bill_date != ', NULL)
                    ->from('app_admin a')
                    ->join('app_bill b', 'a.customer_id=b.customer_id')
                    ->join('app_bill_month m', 'b.id=m.bill_id')
                    ->limit($listRows, $firstRow)->get();
            }
            $bill = $bill->result_array();
            $bill['bill'] = $bill;
            $bill['total_page'] = ceil(count($bill) / 10);
            $bill['nowPage'] = $nowPage;
            $bill['preurl'] = $data['preurl'];
            $bill['nexturl'] = $data['nexturl'];
            $bill['status'] = $status;
            $bill['period'] = $period['account_period_status'];
            $this->load->view('news/news_bill_month', $bill);
        }
        //逐次账期
        $bill = array();
        if ($period['account_period_type'] == 2) {
            //状态读取
            if ($_GET) {
                if (isset($_GET['statu'])) {
                    if ($_GET['statu'] == 0 || $_GET['statu'] == 1 || $_GET['statu'] == 2) {
                        $status = $_GET['statu'];
                        $bill = $this->db->select('b.*,a.*')
                            ->where('a.customer_id', $customer_id)
                            ->where('status', $status)
                            ->from('app_admin a')
                            ->join('app_bill b', 'a.customer_id=b.customer_id')
                            ->get();
                    }
                }
            } else {
                $status = 0;
                $bill = $this->db->select('b.*,a.*')
                    ->where('a.customer_id', $customer_id)
                    ->from('app_admin a')
                    ->join('app_bill b', 'a.customer_id=b.customer_id')
                    ->get();
            }
            $bill = $bill->result_array();
            $bill['total'] = count($bill);

            //分页
            $listRows = 2;                    //每页显示条数
            $num = ceil($bill['total'] / $listRows);
            if ($num = floor($num)) {
                $num = floor($num);
            } else {
                $num = floor($num) + 1;
            }
            $nowPage = isset($_GET['p']) ? $_GET['p'] : 1;  //当前页
            if ($nowPage < $num) {                                              //下一页
                $data['nexturl'] = base_url('news/news_bill_every') . '?p=' . ($nowPage + 1) . '&statu=' . $status;
//                $data['nexturl'] = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?p=' . ($nowPage + 1) . '&statu=' . $status;
            } else {
                $data['nexturl'] = 'javascript:;';
            }
            if ($nowPage > 1) {                              //上一页
                $data['nexturl'] = base_url('news/news_bill_every') . '?p=' . ($nowPage - 1) . '&statu=' . $status;
//                $data['preurl'] = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . '?p=' . ($nowPage - 1) . '&statu=' . $status;
            } else {
                $data['preurl'] = 'javascript:;';
            }
            $firstRow = $listRows * ($nowPage - 1); //后一页
            if ($_GET) {
                if (isset($_GET['statu'])) {
                    if ($_GET['statu'] == 0 || $_GET['statu'] == 1 || $_GET['statu'] == 2) {
                        $status = $_GET['statu'];
                        $bill = $this->db->select('b.*,a.*')
                            ->where('a.customer_id', $customer_id)
                            ->where('status', $status)
                            ->from('app_admin a')
                            ->join('app_bill b', 'a.customer_id=b.customer_id')
                            ->limit($listRows, $firstRow)->get();
                    }
                }
            } else {
                $status = 0;
                $bill = $this->db->select('b.*,a.*')->where('a.customer_id', $customer_id)
                    ->where('status', $status)
                    ->from('app_admin a')
                    ->join('app_bill b', 'a.customer_id=b.customer_id')
                    ->limit($listRows, $firstRow)->get();
            }
            $bill = $bill->result_array();
            $bill['bill'] = $bill;
            $bill['total_page'] = ceil(count($bill) / 10);
            $bill['nowPage'] = $nowPage;
            $bill['preurl'] = $data['preurl'];
            $bill['nexturl'] = $data['nexturl'];
            $bill['status'] = $status;
            $bill['period'] = $period['account_period_status'];
            $this->load->view('news/news_bill_every', $bill);
        }
        if (!isset($period)) {
            $bill['period'] = 0;
            $this->load->view('news/news_bill_every', $bill);
        }
    }

}